package antoine.ear.keyboard;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JPanel;

import antoine.ear.GUI;
import antoine.ear.Main;
import antoine.ear.Player;

public class Keyboard extends JPanel{

	private Map notes;
    private Note activeNote;
    
    
    public void paintComponent(Graphics g) {
		clear(g);
		Graphics2D g2d = (Graphics2D)g;
		KeyboardGraphic kbg = new KeyboardGraphic(g2d);
		
		kbg.drawKeyboard(this);
	}
  	
	protected void clear(Graphics g) {
		super.paintComponent(g);
	}

	public Map getNotes() {
		return notes;
	}

	public void setNotes(Map notes) {
		this.notes = notes;
	}
	
	public void activateNote(int note, boolean activate){
		GUI gui = GUI.getInstance();
		if (gui.soluceMode){
			note = note-11;
			Note notte = (Note) notes.get(new Integer(note));
			notte.setActive(activate);
			this.repaint();			
		}
	}
	
    public void noteClicked(int note, boolean activate) {
        note = note-11;
        Note notte = (Note) notes.get(new Integer(note));
        notte.setActive(activate);
        this.repaint();     
    }
    
	public void clearKeyb(){
		Iterator ite = this.getNotes().values().iterator();
		while (ite.hasNext()){
			Note note = (Note) ite.next();
			note.setActive(false);
		}
		this.repaint();
	}
	
	public void initialize(){
        this.addMouseListener(new KeyboardListener(this));

		notes = new HashMap();
		int offset = 20;
		int octave = 0;
		for (int i=1;i<8;i++){
			offset = 20+(i-1)*77;
			
			Note note = new Note(octave+12);
			note.setName("C");
			note.addPoint(0+offset, 50);
			note.addPoint(0+offset, 0);
			note.addPoint(8+offset, 0);
			note.addPoint(8+offset, 29);
			note.addPoint(10+offset, 29);
			note.addPoint(10+offset, 50);
			notes.put(new Integer(octave+1), note);
			
			note = new Note(octave+13);
			note.setName("Db");
			note.setBlack(true);
			note.addPoint(8+offset, 0);
			note.addPoint(8+offset, 28);
			note.addPoint(14+offset, 28);
			note.addPoint(14+offset, 0);
			notes.put(new Integer(octave+2), note);
			
			note = new Note(octave+14);
			note.setName("D");
			note.addPoint(11+offset, 29);
			note.addPoint(11+offset, 50);
			note.addPoint(21+offset, 50);
			note.addPoint(21+offset, 29);
			note.addPoint(19+offset, 29);
			note.addPoint(19+offset, 0);
			note.addPoint(15+offset, 0);
			note.addPoint(15+offset, 29);
			notes.put(new Integer(octave+3), note);
			
			note = new Note(octave+15);
			note.setName("Eb");
			note.setBlack(true);
			note.addPoint(19+offset, 0);
			note.addPoint(19+offset, 28);
			note.addPoint(25+offset, 28);
			note.addPoint(25+offset, 0);
			notes.put(new Integer(octave+4), note);
			
			note = new Note(octave+16);
			note.setName("E");
			note.addPoint(26+offset, 0);
			note.addPoint(26+offset, 29);
			note.addPoint(22+offset, 29);
			note.addPoint(22+offset, 50);
			note.addPoint(32+offset, 50);
			note.addPoint(32+offset, 00);
			notes.put(new Integer(octave+5), note);
					
			note = new Note(octave+17);
			note.setName("F");
			note.addPoint(33+offset, 50);
			note.addPoint(33+offset, 0);
			note.addPoint(41+offset, 0);
			note.addPoint(41+offset, 29);
			note.addPoint(43+offset, 29);
			note.addPoint(43+offset, 50);
			notes.put(new Integer(octave+6), note);
			
			note = new Note(octave+18);
			note.setName("Gb");
			note.setBlack(true);
			note.addPoint(41+offset, 0);
			note.addPoint(41+offset, 28);
			note.addPoint(47+offset, 28);
			note.addPoint(47+offset, 0);
			notes.put(new Integer(octave+7), note);
			
			note = new Note(octave+19);
			note.setName("G");
			note.addPoint(44+offset, 29);
			note.addPoint(44+offset, 50);
			note.addPoint(54+offset, 50);
			note.addPoint(54+offset, 29);
			note.addPoint(52+offset, 29);
			note.addPoint(52+offset, 0);
			note.addPoint(48+offset, 0);
			note.addPoint(48+offset, 29);
			notes.put(new Integer(octave+8), note);
			
			note = new Note(octave+20);
			note.setName("Ab");
			note.setBlack(true);
			note.addPoint(52+offset, 0);
			note.addPoint(52+offset, 28);
			note.addPoint(58+offset, 28);
			note.addPoint(58+offset, 0);
			notes.put(new Integer(octave+9), note);
			
			note = new Note(octave+21);
			note.setName("A");
			note.addPoint(59+offset, 0);
			note.addPoint(59+offset, 29);
			note.addPoint(55+offset, 29);
			note.addPoint(55+offset, 50);
			note.addPoint(65+offset, 50);
			note.addPoint(65+offset, 29);
			note.addPoint(63+offset, 29);
			note.addPoint(63+offset, 0);
			notes.put(new Integer(octave+10), note);
			
			note = new Note(octave+22);
			note.setName("Bb");
			note.setBlack(true);
			note.addPoint(63+offset, 0);
			note.addPoint(63+offset, 28);
			note.addPoint(69+offset, 28);
			note.addPoint(69+offset, 0);
			notes.put(new Integer(octave+11), note);
			
			note = new Note(octave+23);
			note.setName("B");
			note.addPoint(69+offset, 0);
			note.addPoint(69+offset, 29);
			note.addPoint(66+offset, 29);
			note.addPoint(66+offset, 50);
			note.addPoint(76+offset, 50);
			note.addPoint(76+offset, 0);
			notes.put(new Integer(octave+12), note);
			octave = octave+12;
		}
	}
    
    public Note getActiveNote() {
        return activeNote;
    }

    public void setActiveNote(Note activeNote) {
        this.activeNote = activeNote;
    }
}
